/**
 * gruntfile文件的编写
 */

const sass = require("sass");

module.exports = grunt => {
  grunt.initConfig({
    clean: {
      temp: ".grunttemp/**",
      dist: "gruntdist/**",
      tmp: ".tmp/**"
    },
    //css的构建
    sass: {
      styles: {
        options: {
          implementation: sass
        },
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["assets/styles/*.scss"],
            dest: ".grunttemp/",
            ext: ".css"
          }
        ]
      }
    },
    //babel转换
    babel: {
      options: {
        presets: ["@babel/preset-env"]
      },
      scripts: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["assets/scripts/*.js"],
            dest: ".grunttemp/"
          }
        ]
      }
    },
    // 模板信息拼接
    web_swig: {
      options: {
        swigOptions: {
          cache: false
        },
        getData: function(tpl) {
          return {
            menus: [
              {
                name: "Home",
                icon: "aperture",
                link: "index.html"
              },
              {
                name: "About",
                link: "about.html"
              },
              {
                name: "Contact",
                link: "#",
                children: [
                  {
                    name: "Twitter",
                    link: "https://twitter.com/w_zce"
                  },
                  {
                    name: "Baidu",
                    link: "https://baidu.com"
                  },
                  {
                    name: "divider"
                  },
                  {
                    name: "Github",
                    link: "https://github.com"
                  }
                ]
              }
            ],
            pkg: require("./package.json"),
            date: new Date()
          };
        }
      },
      pages: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["*.html"],
            dest: ".grunttemp/"
          }
        ]
        // Target-specific file lists and/or options go here.
      }
    },
    //图片压缩
    imagemin: {
      images: {
        options: {
          optimizationLevel: 3
        },
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["assets/images/*.{png,jpg,gif,svg}"],
            dest: "gruntdist/"
          }
        ]
      }
    },
    //把字体公共文件复制过去
    copy: {
      fonts: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["assets/fonts/**"],
            dest: "gruntdist/"
          }
        ]
      },
      public: {
        files: [
          {
            expand: true,
            cwd: "public",
            src: ["**"],
            dest: "gruntdist/"
          }
        ]
      },
      pages: {
        files: [
          {
            expand: true,
            cwd: ".grunttemp",
            src: ["*.html"],
            dest: "gruntdist/"
          }
        ]
      }
    },
    //监听文件的改变
    watch: {
      css: {
        files: ["src/assets/styles/*.scss"],
        tasks: ["sass"]
      },
      js: {
        files: ["src/assets/scripts/*.js"],
        tasks: ["babel"]
      },
      html: {
        files: ["src/*.html"],
        tasks: ["web_swig"]
      }
    },
    //启动浏览器
    browserSync: {
      bsFiles: {
        src: ".grunttemp/**"
      },
      options: {
        watchTask: true,
        server: {
          baseDir: [".grunttemp", "src", "public"],
          routes: {
            "/node_modules": "node_modules"
          }
        }
      }
    },
    //分析代码中的引用
    useminPrepare: {
      html: [".grunttemp/*.html"],
      options: {
        dest: "gruntdist",
        // staging: ".temp",
        root: [".grunttemp/", "./"]
      }
    },
    //用绝对路径进行替换
    usemin: {
      html: [".grunttemp/*.html"],
      options: {
        assetsDirs: [".grunttemp/", ".", "node_modules"]
      }
    },
    //压缩css
    cssmin: {
      target: {
        files: [
          {
            expand: true,
            cwd: ".tmp/concat",
            src: ["assets/styles/main.css"],
            dest: "gruntdist/"
          }
        ]
      },
      target2: {
        files: [
          {
            expand: true,
            cwd: ".tmp/concat",
            src: ["assets/styles/vendor.css"],
            dest: "gruntdist/"
          }
        ]
      }
    }
  });

  grunt.loadNpmTasks("grunt-contrib-clean");
  grunt.loadNpmTasks("grunt-sass");
  grunt.loadNpmTasks("grunt-babel");
  grunt.loadNpmTasks("grunt-web-swig");
  grunt.loadNpmTasks("grunt-contrib-imagemin");
  grunt.loadNpmTasks("grunt-contrib-copy");
  grunt.loadNpmTasks("grunt-browser-sync");
  grunt.loadNpmTasks("grunt-contrib-watch");
  grunt.loadNpmTasks("grunt-usemin");
  grunt.loadNpmTasks("grunt-contrib-uglify");
  grunt.loadNpmTasks("grunt-contrib-cssmin");
  grunt.loadNpmTasks("grunt-contrib-concat");
  grunt.loadNpmTasks("grunt-useref");

  grunt.registerTask("compile", ["sass", "babel", "web_swig"]);

  grunt.registerTask("serve", ["compile", "browserSync", "watch"]);

  grunt.registerTask("build", [
    "clean",
    "compile",
    "imagemin",
    "copy:fonts",
    "copy:public",
    "resource"
  ]);

  grunt.registerTask("resource", [
    "useminPrepare",
    "concat:generated",
    // "cssmin:generated",
    "uglify:generated",
    "cssmin:target",
    "cssmin:target2",
    "usemin",
    "copy:pages"
  ]);
  //   grunt.registerTask("styles", () => {});
};
